#pragma once

#include <memory>
#include <unordered_map>
#include <typeindex>
#include <functional>
#include "component.h"

namespace kanon
{
    class Actor 
    {
    private:
        std::unordered_map<std::type_index, std::unique_ptr<Component> > components;
        uint32_t id;

    public:
        Actor(uint32_t id = 0)
        {
            this->id = id;
        }

        template<typename T, typename... Args>
        T& AddComponent(Args&&... args) 
        {
            auto component = std::make_unique<T>(std::forward<Args>(args)...);
            component->owner = this;
            auto& ref = *component;
            components[typeid(T)] = std::move(component);
            return ref;
        }

        template<typename T>
        bool HasComponent() const 
        {
            return components.count(typeid(T)) > 0;
        }

        template<typename T>
        T& GetComponent() 
        {
            return *static_cast<T*>(components[typeid(T)].get());
        }
    };
}